﻿using System;
using System.Collections.Generic;
using AutoMapper;
using BestTrader.Cms.IServices;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.ViewModels.WebApi;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace BestTrader.Cms.WebApi.Controllers.v1
{
    /// <summary>
    /// 返佣提现记录
    /// </summary>
    [ApiVersion("1.0")]
    [Route("api/v{version:apiVersion}/[controller]")]
    [ApiController]
    public class RebateExtractController : BaseController
    {

        private readonly IRebateExtractService _service;
        private readonly IManagerService _managerService;
        private readonly IMapper _mapper;

        public RebateExtractController(IRebateExtractService service, IManagerService managerService, IMapper mapper)
        {
            _service = service;
            _managerService = managerService;
            _mapper = mapper;
        }
        /// <summary>
        /// [10001] 返佣提现记录
        /// </summary>
        /// <remarks>
        /// RebateExtractRecordInput 返佣提现记录:
        /// INPUT: /RebateExtractRecordInput
        /// {
        ///  "currentId": 0,    //当前用户Id
        ///  "roleId": 0,       //角色ID
        ///  "start": "2019-02-26T05:45:31.694Z",
        ///  "end": "2019-02-26T05:45:31.694Z",
        ///  "page": 0,
        ///  "limit": 0,
        ///  "Key": "",// 关键字
        ///  "State": "" // 状态 0:待审核,1：提现成功,2：提现取消,3：提现失败  ,空值则是全部
        /// }
        /// OUTPUT /LoginOutput
        /// {
        ///  "id": 54,
        ///  "managerId": 75,
        ///  "managerName": "bt004",
        ///  "managerRealName": "郑少秋",
        ///  "roleId": 4,
        ///  "bankType": null,      //开户银行
        ///  "bankProvince": null,  //银行卡省份
        ///  "bankCity": null,  //城市
        ///  "bankCard": "31265465498125614631", //银行卡号
        ///  "applyTime": "2019-02-18T14:47:34.447",//申请时间
        ///  "auditTime": null, //审核时间
        ///  "extractRMBMoney": 1500,   //提现金额
        ///  "state": 1  // 状态 0:待审核,1：提现成功,2：提现取消,3：提现失败 
        ///}
        /// </remarks>
        /// <returns></returns>
        [HttpPost("GetRebateExtractRecord")]
#if !DEBUG
        [Authorize]

#endif
        public JsonResult GetRebateExtractRecord(RebateExtractRecordInput input)
        {
            var model = _mapper.Map<RebateExtractRequestModel>(input);
            if (input.Limit == 0) model.Limit = 10;
            if (input.Page == 0) model.Page = 1;

            var result = _service.LoadData(model, input.CurrentId.ToString(), input.RoleId.ToString());

            return Json<Object>(new { data = result.data, count = result.count }, true, result.code, result.msg);

        }
        /// <summary>
        /// [10002] 申请提现
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("ApplyRebateExtract")]
        [Authorize]
        public JsonResult ApplyRebateExtract(ApplyRebateExtractInput input)
        {
            var result = new BaseResult() { Success = false };
            var model = _mapper.Map<RebateExtractAddOrModifyModel>(input);
            var TotalMoney = _service.GetTotalMoneyByManagerId(input.ManagerId.ToString());
            if (TotalMoney < input.ExtractRMBMoney)
            {
                result.ResultCode = 101;
                result.ResultMsg = "余额不足！";
            }
            else
            {
                result = _service.AddOrModify(model, input.ManagerId.ToString());
                if (result.ResultCode == 200) result.Success = true;
            }

            return Json<Object>("", result.Success, result.ResultCode, result.ResultMsg);

        }
        /// <summary>
        /// [10003] 我的可提现余额
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("GetTotalMoneyByManagerId")]
        [Authorize]
        public JsonResult GetTotalMoneyByManagerId(ManagerIdInput input)
        {
            var result = new BaseResult();
            var data = _service.GetTotalMoneyByManagerId(input.Id.ToString());

            return Json<Object>(data, result.Success, result.ResultCode, result.ResultMsg);

        }

        //_service.GetTotalMoneyByManagerId(UserId);

    }
}